##
## Makefile for  in /home/wild/ARDUINO
## 
## Made by Anthony Fournier
## Login   <anthony.fournier@gmx.com>
## 
## Started on  Sun Jul 10 01:43:11 2011 Anthony Fournier
## Last update Mon Aug 15 21:31:37 2011 Anthony Fournier
##

BIN      =	firmware
ELF	 =	$(BIN).elf
HEX	 =	$(BIN).hex

PATHSRC	 =	./src
INCSRC	 =	./include

SRCS     =	$(PATHSRC)/setup.cpp		\
		$(PATHSRC)/loop.cpp		\

LIB	 =	core.a

OBJS     = 	$(SRCS:.cpp=.o)

GCC      =	avr-gcc
GPP      =	avr-g++
OBJCPY	 =	avr-objcopy

RM	 =	rm -f

LIBDIR	 =	./lib
INCLUDE  = 	-I$(LIBDIR)/include

MMCU	 =	-mmcu=atmega328p

OS	 =	-Os

CPPFLAGS =      $(OS)			\
		-w 			\
		-fno-exceptions 	\
		-ffunction-sections	\
		-fdata-sections		\
		$(MMCU)			\
		-DF_CPU=16000000L	\
		-DARDUINO=22

LDFLAGS	 = 	$(OS)			\
		-Wl,--gc-sections	\
		$(MMCU)			\
		$(LIBDIR)/$(LIB) -L lib -lm

DEVICE   =	/dev/ttyACM0

BITRATE  =	115200

all: 	$(HEX)


%.o: 	%.cpp
	$(GPP) -c $< $(INCLUDE) -I$(INCSRC) $(CPPFLAGS) -o $@

%.elf: 	%.o


$(HEX): $(OBJS)
	make -C $(LIBDIR)
	$(GCC) -o $(ELF) $(OBJS) $(LDFLAGS)
	$(OBJCPY) -O ihex -R .eeprom $(ELF) $(HEX)

core:
	make -C $(LIBDIR)

upload: all
	pkill screen ; if [ $$? -eq 0 ] ; then sleep 1 ; fi ; avrdude -v -p m328p -P $(DEVICE) -b$(BITRATE) -c arduino -U flash:w:$(HEX)

out:
	screen $(DEVICE) $(BITRATE) ; reset ; clear

clean:
	$(RM) $(OBJS) $(ELF)

fclean: clean
	$(RM) $(HEX)

re: 	fclean all
